package com.wavejo.reactor;

import com.wavejo.reactor.pojo.User;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

/**
 * @Author WaveHi
 * @Date 28/10/2021 下午 1:27
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
public class Client {

    public static void main(String[] args) {

        //根据地址获取服务器
        WebClient webClient = WebClient.create("http://localhost:12442");

        //获取请求参数
        String id = "1";

        //访问服务器获取数据
        User user = webClient.get().uri("/users/{id}", id)
                .accept(MediaType.APPLICATION_JSON).retrieve()
                .bodyToMono(User.class)
                .block();

        //输出
        System.out.println(user);

        Flux<User> userFlux = webClient.get().uri("/users")
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToFlux(User.class);
        userFlux.map(User::getUsername)
                .buffer().doOnNext(System.out::println)
                .blockFirst();
    }
}
